// pages/Purchasing/Monthly/Monthly.js
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Purchasing: '', //首次置業
    MCinmate: '',//澳門居民
    installment: '',//每月供款能力
    firstphase: '',//首期
    annualrate: '',//年利率
    repaymentyear: '',//還款年期
    statisticsArray: [],
    Moyouth: '',
    showPurchasing: false,
    showMoyouth: false,
    houseType: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.calculate();





  },
  calculate: function () {
    var _showPurchasing = false;
    var _showMoyouth = false;
    var kPurchasing = wx.getStorageSync('kPurchasing');//首次置業
    var kMCinmate = wx.getStorageSync('kMCinmate');//澳門居民
    var kinstallment = parseFloat(wx.getStorageSync('kinstallment'));//每月供款能力
    var kfirstphase = wx.getStorageSync('kfirstphase');//首期
    var kannualrate = parseFloat(wx.getStorageSync('kannualrate'));//年利率
    var krepaymentyear = wx.getStorageSync('krepaymentyear');//還款年期
    var Moyouth = wx.getStorageSync('kfirstMoyouth');//是否青年职业
    var calculateType = wx.getStorageSync('kfirstcalculateType');//是否楼花
    var _mcinmate = ''; var _purchasing = ''; var _firstMoyouth = '否'; var _calculateType = '';
    if (kMCinmate == 'MC1')
      _mcinmate = '是'
    if (kMCinmate == 'MC2') {
      _mcinmate = '否'
      _showPurchasing = true;
      _showMoyouth = true;
    }

    if (kPurchasing == 'first')
      _purchasing = '是'
    if (kPurchasing == 'nofirst') {
      _purchasing = '否'
      _showMoyouth = true;
    }
    if (Moyouth == 'youth')
      _firstMoyouth = '是'

    if (calculateType == 'Real')
      _calculateType = '否'
    if (calculateType == 'Stage')
      _calculateType = '是'


    var repaymenmonth = parseInt(krepaymentyear) * 12//期數


    var resultArray = this.GetNewMonthly(kinstallment, kannualrate, krepaymentyear, repaymenmonth);

    //  var resultArray = this.GetMonlyList(kannualrate, repaymenmonth, kinstallment, kfirstphase, kMCinmate, kPurchasing, Moyouth, calculateType, krepaymentyear)
    //resultArray = resultArray.sort('houseprice').reverse();
    this.setData({
      Purchasing: _purchasing, //首次置業
      MCinmate: _mcinmate,//澳門居民
      installment: '$' + util.formatCurrency(parseFloat(kinstallment)),//每月供款能力
      firstphase: '$' + util.formatCurrency(parseFloat(kfirstphase) * 10000),//首期
      annualrate: kannualrate + '%',//年利率
      repaymentyear: krepaymentyear + '年(' + repaymenmonth + '期)',//還款年期
      statisticsArray: resultArray,
      showPurchasing: _showPurchasing,
      Moyouth: _firstMoyouth,
      houseType: _calculateType
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /*新计算方式*/
  GetNewMonthly: function (kinstallment, kannualrate, krepaymentyear, repaymenmonth) {
    var resultArray = new Array();
    //kinstallment 月供
    var monthrate = kannualrate / 100 / 12; //月利率
    parseInt(krepaymentyear) * 12//期數
    var loan = (kinstallment * (Math.pow(1 + monthrate, repaymenmonth) - 1)) / (monthrate * (Math.pow(1 + monthrate, repaymenmonth)))
    //console.log(loan)
    for (var j = 0; j <= 9;j++) {
    
      var nodigithouseprice = 0;
      //var percent = (loan / houseprice);
       //console.log(percent+"****************")
      var perloan = (loan / 10000).toFixed(0) + '萬(' + (1-j/10).toFixed(2)*100+'%)';
      
      var houseprice = loan / (1 - (j / 10));//樓價
      nodigithouseprice = (houseprice / 10000).toFixed(0) +'萬';
      var needfirst = ((houseprice - loan) / 10000).toFixed(0) + '萬';
      
     // console.log((loan / houseprice).toFixed(2))
      
     
      var s = { nodigithouseprice, perloan, needfirst }
      resultArray.push(s)
    }
    return resultArray;
  },


  
  /*旧计算方式*/
  GetMonlyList: function (kannualrate, repaymenmonth, kinstallment, kfirstphase, kMCinmate, kPurchasing, Moyouth, calculateType, krepaymentyear) {
    var monthrate = kannualrate / 100 / 12; //月利率


    //按揭
    var PV = (kinstallment * (Math.pow(1 + monthrate, repaymenmonth) - 1)) / (monthrate * Math.pow(1 + monthrate, repaymenmonth))
    var fistcount = PV + kfirstphase * 10000 //最符合的那一條

    var resultArray = new Array();
    for (var i = 1; i <= 9; i++) {
      var _pv = ''
      if (PV >= 10000) {
        _pv = (PV / 10000).toFixed(0) + '萬(' + (i * 10) + '%)';
      }
      else {
        _pv = PV.toFixed(0) + '元(' + (i * 10) + '%)';
      }
      var houseprice = PV / (i / 10);//樓價
      var nodigithouseprice = ''; //houseprice.toFixed(0)
      if (houseprice >= 10000)
        nodigithouseprice = util.formatCurrency(Math.ceil((houseprice / 10000))) + '萬' //有小數就像上取整
      else
        nodigithouseprice = (houseprice).toFixed(0) + '元'

      var firstprice = houseprice - PV;//首期
      var nodigitfirstprice = ''//firstprice.toFixed(0);//四捨五入取整
      if (firstprice >= 10000)
        nodigitfirstprice = util.formatCurrency(Math.ceil(((firstprice / 10000).toFixed(0)))) + '萬(' + (100 - i * 10) + '%)'
      else
        nodigitfirstprice = firstprice.toFixed(0) + '元(' + (100 - i * 10) + '%)'


      var per = util.JudgementHoust(houseprice, kMCinmate, kPurchasing, Moyouth, calculateType) * 10;//正常區間取比例
      var fisetper = util.JudgementHoust(fistcount, kMCinmate, kPurchasing, Moyouth, calculateType) * 10;//單獨紅色區間取比例

      if (i <= per) {
        if (fisetper <= per) {
          var s = { "cKey": 'n', nodigithouseprice, nodigitfirstprice, _pv, per, firstprice, houseprice }
          resultArray.push(s)
        }
      }

    }

    //篩選小於當前最合適金額的列表
    var query = resultArray.filter(function (x) {
      return parseFloat(x.houseprice) <= parseFloat(fistcount);
    });

    resultArray = query;
    if (resultArray.length <= 0)//沒有一個符合  //跳轉至按首期預算界面
    {


      wx.setStorageSync('kfirstphaseText', kfirstphase)
      wx.setStorageSync('kfirstphaserate', kannualrate)
      wx.setStorageSync('kfirstphaseyear', krepaymentyear)
      wx.setStorageSync('kfirstMCinmate', kMCinmate)
      wx.setStorageSync('kfirstPurchasing', kPurchasing)
      wx.setStorageSync('Moyouth', Moyouth);//是否青年职业
      wx.setStorageSync('calculateType', calculateType);//是否楼花
      wx.showLoading({
        title: '計算中',
        success: function (e) {

        }
      })
      setTimeout(function () {
        wx.hideLoading();
        wx.redirectTo({
          url: '/pages/Purchasing/firstphase/firstphase',
        })
      }, 2000)
      return false;
    }
    else {


      //往頭部插入一條
      if (fistcount >= 10000)
        nodigithouseprice = util.formatCurrency(Math.ceil((fistcount / 10000))) + '萬'
      else
        nodigithouseprice = util.formatCurrency((fistcount).toFixed(0)) + '元'

      var headprice = fistcount - PV;//首期
      var nodigitfirstprice = ''
      var hp = Math.ceil(((headprice / fistcount) * 100).toFixed(1)) //s首期占百分比

      if (headprice >= 10000)
        nodigitfirstprice = util.formatCurrency(Math.ceil(((headprice / 10000).toFixed(0)))) + '萬(' + (hp) + '%)'
      else
        nodigitfirstprice = util.formatCurrency(headprice.toFixed(0)) + '元(' + (hp) + '%)'


      var p = (100 - hp);
      if (PV >= 10000) {
        _pv = (PV / 10000).toFixed(0) + '萬(' + p + '%)';
      }
      else {
        _pv = PV.toFixed(0) + '元(' + p + '%)';
      }

      //resultArray.unshift({ "cKey": 'u', nodigithouseprice, nodigitfirstprice, _pv })

    }


    return resultArray;



  }
})